'use strict';
const path = require('path');
const fs = require('fs');
const BaseController = require('./BaseController');
class AudioController extends BaseController {
  async uploadpictures() {
    const { ctx, app } = this;
    let img_key = "";
    try{
      let file =ctx.request.files[0];
      if(!file){
        ctx.body = {
          code:0,
          msg:"请上传图片!"
        }
        return;
      }
      if(!file.mime.includes("image")){
        ctx.body = {
          code:0,
          msg:"不支持的文件格式"
        }
        return;
      }
      let f = fs.readFileSync(file.filepath) //files[0]表示获取第一个文件，若前端上传多个文件则可以遍历这个数组对象
      let dir = this.config.static.dir+"/img"
      let filename = "/"+Date.now()+path.extname(file.filename)
      let uploadDir = dir+filename;
      img_key = this.config.static.prefix+"/img"+filename
      fs.writeFileSync(uploadDir, f)
    }catch(err){
      console.log(err)
    }
    ctx.body = {
      code:1,
      data:{
        img_key,
        filename:ctx.request.files[0].filename.split(".")[0]
      }
    }
  }
  async uploadMedia() {
    const { ctx, app } = this;
    let resultData = {}
    let fileName = "";
    try{
      let folderName = ctx.request.body.folderName;
      if(!folderName){
        ctx.body = {
          code:0,
          msg:"请指定文件夹！！",
        }
        return;
      }
      let parentDir = fs.readdirSync(this.config.static.dir);
      if(!parentDir.includes(folderName)){
        ctx.body = {
          code:0,
          msg:"没有"+folderName+"这个文件夹"
        }
        return;
      }
      folderName = "/"+folderName+"/"
      let file =ctx.request.files[0];
      console.log(file)
      if(!file.mime.includes("audio") && !file.mime.includes("video") ){
        ctx.body = {
          code:0,
          msg:"不支持的文件格式"
        }
        return;
      }
      let f = fs.readFileSync(file.filepath)
      let dir = this.config.static.dir+folderName;
      
      const fileList = fs.readdirSync(dir)
      if(fileList.includes(file.filename)){
        ctx.body = {
          code:0,
          msg:"已存在相同文件，请修改文件信息再上传"
        }
        return;
      }
      let uploadDir = dir+file.filename;
      fileName = file.filename.split(".")[0];
      fileName = fileName.split("-");
      const key = this.config.static.prefix+folderName+file.filename
      fs.writeFileSync(uploadDir, f)
      console.log(folderName)
      if(folderName.indexOf("sound")!=-1){
        resultData.audio_key = key
        resultData.audioName = fileName[0]
      }else{
        resultData.video_key = key
        resultData.videoName = fileName[0]
      }
      console.log(resultData)
      resultData.author = fileName[1]
    }catch(err){
      console.log(err)
    }
    ctx.body = {
      code:1,
      data:resultData
    }
  }
  async deleteMedia(){
    const { ctx, app } = this;
    const filename = ctx.request.body.filename;
    const result = await ctx.service.media.deleteMedia({url:filename});
    ctx.body = result;
  }
}

module.exports = AudioController;
